package com.huobao.yunying.activitys;

import java.util.List;
import java.util.Map;

import org.xutils.view.annotation.ViewInject;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.huobao.yunying.adapters.Adapter_jinritehui;
import com.huobao.yunying.base.HuobaoBaseActivity;
import com.huobao.yunying.entity.Jinritehui;
import com.huobao.yunying.https.Http;
import com.huobao.yunying.https.HttpConst;
import com.huobao.yunying.https.HttpListener;
import com.huobao.yunying.https.HttpUtil;
import com.huobao.yunying.utils.ImageUtils;
import com.huobao.yunying.utils.ImageUtils.OnImageLoadListener;
import com.huobao.yunying.utils.SkipUtils;
import com.huobao.yunying.views.NoScrollGridView;
import com.huobao.yunying.views.ObservableScrollView;
import com.huobao.yunying.views.ObservableScrollView.ScrollViewListener;
import com.huobao.zhangying.R;
import com.xrz.sxm.base.utils.ScreenUtil;

import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;

/**
 * 今日特惠
 * 
 * @author Administrator
 *
 */
public class Act_Jinritehui extends HuobaoBaseActivity implements ScrollViewListener,OnItemClickListener,OnClickListener{
	@ViewInject(R.id.scrollview)
	private ObservableScrollView scrollview;
	// 头部图片
	@ViewInject(R.id.imageview)
	private ImageView imageview;
	@ViewInject(R.id.gridview)
	private NoScrollGridView gridview;

	private Adapter_jinritehui adapter;

	private int newHight;
	@ViewInject(R.id.btn_totop)
	private ImageView btn_totop;
	@Override
	protected int getContentView() {
		return R.layout.layout_jinritehui;
	}

	@Override
	protected String getTitleText() {
		return null;
	}

	@Override
	protected void init() {
		btn_totop.setOnClickListener(this);
		scrollview.setScrollViewListener(this);
		mAbTitleBar.setVisibility(View.GONE);
		adapter = new Adapter_jinritehui(context);
		gridview.setAdapter(adapter);
		gridview.setOnItemClickListener(this);
		getData();
	}

	private void getData() {
		Map<String, String> map = HttpUtil.getParams(HttpConst.FRIST_KEY, HttpConst.FRIST_VALUE, HttpConst.SECOND_KEY,
				"thgoods");
		Http.getInstant().sendRequest(context, map, new HttpListener() {
			
			@Override
			public void onHttpSuccess(String result) {
				JSONObject jsonObject = JSON.parseObject(result);
				jsonObject = jsonObject.getJSONObject("retval");
				JSONArray array = jsonObject.getJSONArray("sbanber");
				final List<Jinritehui> list = JSON.parseArray(jsonObject.getString("sgoodslist"), Jinritehui.class);
				String spic = array.getJSONObject(0).getString("spic");
				ImageUtils.loadimg(imageview, spic, new OnImageLoadListener() {
					
					@Override
					public void onLoadSuccess(Drawable drawable) {
						int newWidth = ScreenUtil.getInstant().getDisWidth(context);
						newHight = newWidth * drawable.getIntrinsicHeight() / drawable.getIntrinsicWidth();
						LayoutParams params = (LayoutParams) imageview.getLayoutParams();
						params.width = newWidth;
						params.height = newHight;
						imageview.setLayoutParams(params);
						adapter.addAll(list);
						scrollview.smoothScrollTo(0, 0);
					}
				});
			}
			
			@Override
			public void onHttpError(String error) {
			}
		});
	}

	@Override
	public void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy) {
		if(y >= newHight){
			btn_totop.setVisibility(View.VISIBLE);
		}else{
			btn_totop.setVisibility(View.GONE);
		}
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.btn_totop:
			scrollview.smoothScrollTo(0, 0);
			break;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		SkipUtils.seeDetail(context, adapter.getItem(position).getGoods_id(), adapter.getItem(position).getDefault_image());
	}
}
